package com.am.server;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.am.builder.AMBuilder;
import com.am.persistent.AMMemoryPersistent;
import com.am.persistent.IAMPersistent;
import com.am.server.amcore.AMJsonOutput;
import com.am.server.json.JsonUtil;
import com.am.words.IWord;
import com.am.words.Word;

public class AMCoreServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private IAMPersistent ps;
    private AMJsonOutput os;
    public AMCoreServlet(){
	super();
	ps = new AMMemoryPersistent();
	os = new AMJsonOutput();
    }
    
    @Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	resp.setContentType("text/html; charset=utf-8");
	String ws = req.getParameter("w");
	PrintWriter out = resp.getWriter();
	AMBuilder amb = new AMBuilder(ps);
	os.clear();
	amb.setOutputService(os);
	IWord w = new Word(ws);
	amb.buildWithWord(w);
	out.println(JsonUtil.strings2Json(os.getAllOutputs()));
	out.flush();
    }
   
}
